package com.fortune.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.hibernate.validator.constraints.NotBlank;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.fortune.base.BaseAPIExcution;
import com.fortune.common.paging.PagingData;
import com.fortune.constants.Constants;
import com.fortune.dto.excution.DStoreOutExcution;
import com.fortune.dto.in.dStoreOut.DStoreOutListDto;
import com.fortune.entity.DUserInfo;
import com.fortune.enums.DStoreOutEnum;
import com.fortune.service.DStoreOutService;
import com.fortune.vo.dStoreOut.DStoreOutInfo;
import com.fortune.vo.dStoreOut.DStoreOutInfoDetail;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import springfox.documentation.annotations.ApiIgnore;

@Api(value = "dStoreOut", description = "经销商仓库出库", tags = {"经销商仓库出库"})
@RestController
@RequestMapping("dStoreOut")
public class DStoreOutController {
	@Autowired
	private DStoreOutService dStoreOutService;
	@ApiOperation(value = "出库单列表", nickname = "dStoreOutList", notes = "", tags={ "@岳希伟" })
	@ApiImplicitParams({
			@ApiImplicitParam(name="page", value="第几页",paramType="query",dataType="int"),
			@ApiImplicitParam(name="length", value="每页容量",paramType="query",dataType="int"),
			@ApiImplicitParam(name="search", value="搜索关键词",paramType="query",dataType="string"),
			@ApiImplicitParam(name="type",value="是否已发货（0已发货1未发货）",paramType="query",dataType="int",allowableValues = "0,1")
	})
    @GetMapping(value = "list",
        produces = { MediaType.APPLICATION_JSON_VALUE })
	public BaseAPIExcution<PagingData<DStoreOutInfo>> list(HttpServletRequest request, @ApiIgnore @Validated DStoreOutListDto dStoreOutListDto) {
		HttpSession session = request.getSession();
		DUserInfo dUserInfo = (DUserInfo) session.getAttribute(Constants.SESSION_D_USER);
		dStoreOutListDto.setPage((dStoreOutListDto.getPage()-1)*10);
		List<DStoreOutInfo> list = dStoreOutService.queryDStoreOutList(dUserInfo,dStoreOutListDto);
		int count = dStoreOutService.queryDStoreOutListCount(dUserInfo,dStoreOutListDto);
		PagingData<DStoreOutInfo> pagingData = new PagingData<>();
		pagingData.setData(list);
		pagingData.setRecordsTotal(count);
		return new DStoreOutExcution<PagingData<DStoreOutInfo>>(pagingData, DStoreOutEnum.SUCCESS);
	}
	
	@ApiOperation(value = "出库单详情", nickname = "dStoreOutDetail", notes = "", tags={ "@岳希伟" })
	@ApiImplicitParam(name="id", value="出库单id",paramType="query",dataType="string",required=true)
    @GetMapping(value = "detail",
        produces = { MediaType.APPLICATION_JSON_VALUE })
	public BaseAPIExcution<DStoreOutInfoDetail> detail(HttpServletRequest request,@Validated @NotBlank(message="出库单id不能为空")String id) {
		HttpSession session = request.getSession();
		DUserInfo dUserInfo = (DUserInfo) session.getAttribute(Constants.SESSION_D_USER);
		DStoreOutInfoDetail detail = dStoreOutService.queryDStoreOutInfoDetail(dUserInfo,id);
		return new DStoreOutExcution<DStoreOutInfoDetail>(detail, DStoreOutEnum.SUCCESS);
	}


	@ApiOperation(value = "验收出库", nickname = "dStoreOutConfirm", notes = "", tags={ "@岳希伟" })
	@ApiImplicitParam(name="id", value="出库单id",paramType="query",dataType="string",required=true)
    @PutMapping(value = "confirm",
        produces = { MediaType.APPLICATION_JSON_VALUE })
	public BaseAPIExcution<Void> confirm(HttpServletRequest request,@Validated @NotBlank(message="出库单id不能为空")String id) {
		HttpSession session = request.getSession();
		DUserInfo dUserInfo = (DUserInfo) session.getAttribute(Constants.SESSION_D_USER);
		int result = dStoreOutService.updateDStoreOutShipType(dUserInfo, id);
		if (result==1) {
			return new DStoreOutExcution<Void>(null, DStoreOutEnum.SUCCESS);
		}
		return new DStoreOutExcution<Void>(null, DStoreOutEnum.FAILURE);
	} 
}
